package cn.com.computer.my.book.service;

import cn.com.computer.common.base.bean.CandyResult;
import cn.com.computer.common.base.excepetion.TransactionException;
import cn.com.computer.common.enums.StatusEnum;
import cn.com.computer.common.utils.helper.ParamUtils;
import cn.com.computer.my.book.dal.domain.OriginalRaidersBookDO;
import cn.com.computer.my.book.dal.mapper.OriginalRaidersBookMapper;
import cn.com.computer.my.book.service.bo.enums.BookFromEnum;
import cn.com.computer.my.book.web.bean.OriginalRaidersBookBean;
import cn.com.computer.my.game.service.bo.emums.GamePlatformEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * Created by WANGXin on 2017/5/20.
 */
@Component
public class OriginalRaidersBookService {

    private static final Logger LOGGER =  LoggerFactory.getLogger(OriginalRaidersBookService.class);

    private static final Integer ZERO = 0;
    private static final String VOL = "VOL.";
    private static final String CREATE_NAME = "WANGXin";
    private static final String SAVE_PATH_PART = "整理完成/";

    @Autowired
    private OriginalRaidersBookMapper originalRaidersBookMapper;
    @Autowired
    private OriginalRaidersBookPath originalRaidersBookPath;

    /**
     * 保存攻略书信息
     * @param originalRaidersBookBean
     * @return
     */
    @Transactional(value = "myGameTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class)
    public CandyResult saveRaiderBook(OriginalRaidersBookBean originalRaidersBookBean){
        CandyResult candyResult = new CandyResult();

        // 1.check data
        CandyResult checkDataInSaveRaiderBookResult = checkDataInSaveRaiderBook(originalRaidersBookBean);
        if (!checkDataInSaveRaiderBookResult.isSuccess()){
            candyResult.setMessage(checkDataInSaveRaiderBookResult.getMessage());
            return candyResult;
        }

        // 2.data preProcess
        CandyResult<OriginalRaidersBookDO> dataPreprocessInSaveRaidersBookResult = dataPreprocessInSaveRaidersBook(originalRaidersBookBean);
        if (!dataPreprocessInSaveRaidersBookResult.isSuccess()){
            LOGGER.error(dataPreprocessInSaveRaidersBookResult.getMessage());
            candyResult.setMessage(dataPreprocessInSaveRaidersBookResult.getMessage());
            return candyResult;
        }
        OriginalRaidersBookDO originalRaidersBookDO = dataPreprocessInSaveRaidersBookResult.getData();

        // 3.data Algorithm
        CandyResult saveRaidersBookCoreAlgorithmResult = saveRaidersBookCoreAlgorithm(originalRaidersBookDO);
        if (!saveRaidersBookCoreAlgorithmResult.isSuccess()){
            LOGGER.error(saveRaidersBookCoreAlgorithmResult.getMessage());
            throw new TransactionException(saveRaidersBookCoreAlgorithmResult.getMessage());
        }

        candyResult.setSuccess(true);
        return candyResult;
    }

    /*
    ====================================================================================================================
     私有方法
    ====================================================================================================================
     */

    /**
     * 数据预处理
     * @param originalRaidersBookBean
     * @return
     */
    private CandyResult<OriginalRaidersBookDO> dataPreprocessInSaveRaidersBook(OriginalRaidersBookBean originalRaidersBookBean){
        CandyResult<OriginalRaidersBookDO> candyResult = new CandyResult();

        String bookPath = SAVE_PATH_PART + originalRaidersBookBean.getBookFrom();
        String version = VOL + originalRaidersBookBean.getBookVersion();
        String gameName = originalRaidersBookBean.getGameName().trim();

        OriginalRaidersBookDO originalRaidersBookDO = new OriginalRaidersBookDO();
        originalRaidersBookDO.setGameName(gameName);
        originalRaidersBookDO.setGamePlatForm(originalRaidersBookBean.getGamePlatForm());
        originalRaidersBookDO.setBookFrom(originalRaidersBookBean.getBookFrom());
        originalRaidersBookDO.setBookVersion(version);
        originalRaidersBookDO.setBookCapacity(originalRaidersBookBean.getBookCapacity());
        originalRaidersBookDO.setBookPath(bookPath);
        originalRaidersBookDO.setCreateBy(CREATE_NAME);
        originalRaidersBookDO.setStatus(StatusEnum.YES.getCode());

        candyResult.setData(originalRaidersBookDO);
        candyResult.setSuccess(true);
        return candyResult;
    }

    /**
     * 保存攻略书
     * @param originalRaidersBookDO
     * @return
     */
    private CandyResult saveRaidersBookCoreAlgorithm(OriginalRaidersBookDO originalRaidersBookDO){
        CandyResult candyResult = new CandyResult();

        Integer saveRaidersBookCount;
        try{
            saveRaidersBookCount = originalRaidersBookMapper.saveRaidersBook(originalRaidersBookDO);
        }catch (Exception e){
            LOGGER.error("保存攻略书异常，异常参数为:[" + ParamUtils.serializable(originalRaidersBookDO) + "]",e);
            candyResult.setMessage("保存攻略书失败");
            return candyResult;
        }
        if (saveRaidersBookCount.equals(ZERO)){
            LOGGER.error("保存攻略书失败，异常参数为:[" + ParamUtils.serializable(originalRaidersBookDO) + "]");
            candyResult.setMessage("保存攻略书失败");
            return candyResult;
        }

        candyResult.setSuccess(true);
        return candyResult;
    }

    /**
     * 保存攻略书时校验参数
     * @param originalRaidersBookBean
     * @return
     */
    private CandyResult checkDataInSaveRaiderBook(OriginalRaidersBookBean originalRaidersBookBean){
        CandyResult candyResult = new CandyResult();

        if (originalRaidersBookBean == null){
            candyResult.setMessage("攻略书信息不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(originalRaidersBookBean.getGameName())){
            candyResult.setMessage("游戏名称不能为空");
            return candyResult;
        }

        if (StringUtils.isEmpty(originalRaidersBookBean.getGamePlatForm())){
            candyResult.setMessage("游戏平台不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(GamePlatformEnum.getName(originalRaidersBookBean.getGamePlatForm()))){
            candyResult.setMessage("游戏平台不能未知");
            return candyResult;
        }

        if (StringUtils.isEmpty(originalRaidersBookBean.getBookFrom())){
            candyResult.setMessage("攻略书来源不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(BookFromEnum.getValue(originalRaidersBookBean.getBookFrom()))){
            candyResult.setMessage("攻略书来源不能未知");
            return candyResult;
        }

        if (StringUtils.isEmpty(originalRaidersBookBean.getBookCapacity())){
            candyResult.setMessage("攻略书容量大小不能为空");
            return candyResult;
        }

        OriginalRaidersBookDO checkOriginalRaidersBookDO = new OriginalRaidersBookDO();
        checkOriginalRaidersBookDO.setGameName(originalRaidersBookBean.getGameName());
        checkOriginalRaidersBookDO.setBookFrom(originalRaidersBookBean.getBookFrom());

        List<OriginalRaidersBookDO> originalRaidersBookDOList;
        try {
            originalRaidersBookDOList = originalRaidersBookMapper.queryRaidersBookByCondition(checkOriginalRaidersBookDO);
        }catch (Exception e){
            LOGGER.error("查询重复的攻略书信息异常，异常数据为:[" + ParamUtils.serializable(checkOriginalRaidersBookDO) + "]",e);
            candyResult.setMessage("查询重复的攻略书信息失败");
            return candyResult;
        }
        if (!CollectionUtils.isEmpty(originalRaidersBookDOList)){
            candyResult.setMessage("有重复的攻略书，不需要再进行保存该攻略书");
            return candyResult;
        }

        candyResult.setSuccess(true);
        return candyResult;
    }
}
